package com.ciei.dpagm.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ciei.dpagm.common.enums.ComparatorEnum;
import com.ciei.dpagm.common.enums.ParameterConfigurationDataTypeEnum;
import com.ciei.dpagm.common.enums.RelationalOperatorEnum;
import com.ciei.dpagm.common.enums.RuleTypeEnum;
import com.ciei.dpagm.common.service.BaseService;
import com.ciei.dpagm.entity.Condition;
import com.ciei.dpagm.entity.Rule;
import com.ciei.dpagm.entity.RuleSet;
import com.ciei.dpagm.entity.Users;
import com.ciei.dpagm.mapper.RuleMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author pp
 * @date 2023/03/10
 */
@Service
public class RuleService extends BaseService<RuleMapper, Rule> {

    /**
     * 根据规则集id列表获取规则列表
     * @param ruleSetIdList
     * @return
     */
    public List<Rule> getListByRuleSetIdList(List<Integer> ruleSetIdList){
        if(ruleSetIdList.isEmpty()){
            return new ArrayList<>();
        }
        LambdaQueryWrapper<Rule> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.in(Rule::getRuleSetId,ruleSetIdList);
        return list(lambdaQueryWrapper);
    }

    /**
     * 根据规则集id列表删除
     * @param ruleSetIdList
     */
    public void removeByRuleSetIdList(List<Integer> ruleSetIdList){
        if(ruleSetIdList.isEmpty()){
            return;
        }
        LambdaQueryWrapper<Rule> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.in(Rule::getRuleSetId,ruleSetIdList);
        remove(lambdaQueryWrapper);
    }

    /**
     * 生成配置规则
     * @param userId
     * @param addConditionList
     * @param parameterConfigurationDataTypeList
     * @param ruleToConditionListData
     * @param dataType
     * @param validValues
     * @param parameterName
     */
    public Rule createConfigurationRule(Integer userId, List<Condition> addConditionList, List<String> parameterConfigurationDataTypeList, Map<Rule, List<Condition>> ruleToConditionListData, String dataType, String validValues, String parameterName, RuleSet ruleSet, String ruleName,String symbol) {
        if(dataType == null || validValues == null){
            return null;
        }
        if(parameterConfigurationDataTypeList.contains(dataType)){
            Rule rule = new Rule();
            if(ruleName == null){
                rule.setRuleName(String.format("参数%s配置规则", parameterName));
            }else{
                rule.setRuleName(ruleName);
            }
            rule.setCreatorId(userId);
            rule.setRuleSetId(ruleSet.getRuleSetId());
            rule.setType(RuleTypeEnum.CONFIGURATION_RULE);
            List<Condition> conditionList = new ArrayList<>();
            if(ParameterConfigurationDataTypeEnum.ENUM.getType().equals(dataType)){
                Condition condition = new Condition();
                condition.setCreatorId(userId);
                condition.setFactMode(symbol);
                condition.setComparator(ComparatorEnum.IN_SET.getId());
                condition.setStandardSpecification(validValues);
                condition.setRuleSetId(ruleSet.getRuleSetId());
                condition.setOrderId(1);
                conditionList.add(condition);
            }else{
                String[] validValuesSplit = validValues.split("~");
                if(validValuesSplit.length == 2){
                    Condition greaterThenOrEqualCondition = new Condition();
                    greaterThenOrEqualCondition.setCreatorId(userId);
                    greaterThenOrEqualCondition.setFactMode(symbol);
                    greaterThenOrEqualCondition.setComparator(ComparatorEnum.GREATER_THAN_EQUAL.getId());
                    greaterThenOrEqualCondition.setStandardSpecification(validValuesSplit[0]);
                    greaterThenOrEqualCondition.setOrderId(1);
                    Condition lessThanOrEqualCondition = new Condition();
                    lessThanOrEqualCondition.setCreatorId(userId);
                    lessThanOrEqualCondition.setFactMode(symbol);
                    lessThanOrEqualCondition.setComparator(ComparatorEnum.LESS_THAN_EQUAL.getId());
                    lessThanOrEqualCondition.setStandardSpecification(validValuesSplit[1]);
                    lessThanOrEqualCondition.setOrderId(2);
                    greaterThenOrEqualCondition.setRuleSetId(ruleSet.getRuleSetId());
                    lessThanOrEqualCondition.setRuleSetId(ruleSet.getRuleSetId());
                    conditionList.add(greaterThenOrEqualCondition);
                    conditionList.add(lessThanOrEqualCondition);
                }
            }
            addConditionList.addAll(conditionList);
            ruleToConditionListData.put(rule,conditionList);
            return rule;
        }
        return null;
    }

    /**
     * 生成依赖规则
     * @param userId
     * @param relationalOperatorPattern
     * @param ruleToConditionListData
     * @param addConditionList
     * @param dependencies
     * @param parameterName
     */
    public Rule createDependenciesRule(Integer userId, Pattern relationalOperatorPattern, Map<Rule, List<Condition>> ruleToConditionListData, List<Condition> addConditionList, String dependencies, String parameterName, RuleSet ruleSet,String ruleName,String symbol) {
        if (StringUtils.isNotBlank(dependencies)) {
            Rule rule = new Rule();
            if(ruleName == null){
                rule.setRuleName(String.format("参数%s依赖规则", parameterName));
            }else{
                rule.setRuleName(ruleName);
            }
            rule.setSatisfy(symbol);
            rule.setCreatorId(userId);
            rule.setRuleSetId(ruleSet.getRuleSetId());
            rule.setType(RuleTypeEnum.DEPENDENCY_RULE);
            Condition condition = new Condition();
            Matcher matcher = relationalOperatorPattern.matcher(dependencies);
            List<String> keyList = new ArrayList<>();
            while (matcher.find()) {
                keyList.add(matcher.group(1));
            }
            condition.setFactMode(String.join(",", keyList));
            condition.setComparator(ComparatorEnum.SATISFY.getId());
            condition.setStandardSpecification(dependencies);
            condition.setRuleSetId(ruleSet.getRuleSetId());
            condition.setOrderId(1);
            condition.setCreatorId(userId);
            List<Condition> conditionList = new ArrayList<>();
            conditionList.add(condition);
            addConditionList.addAll(conditionList);
            ruleToConditionListData.put(rule,conditionList);
            return rule;
        }
        return null;
    }







}
